package com.j2bugzilla.rpc;

import static org.junit.Assert.*;

import static org.hamcrest.CoreMatchers.notNullValue;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.j2bugzilla.base.BugzillaConnector;
import com.j2bugzilla.base.BugzillaException;
import com.j2bugzilla.base.ConnectionException;
import com.j2bugzilla.base.HistoryElement;

@RunWith(Parameterized.class)
public class GetBugHistoryIT {

	@Parameters
	public static List<Object[]> getUrls() {
		List<Object[]> urls = new ArrayList<Object[]>();
		
		urls.add(new Object[]{"https://landfill.bugzilla.org/bugzilla-3.6-branch/", 1058});
		urls.add(new Object[]{"https://landfill.bugzilla.org/bugzilla-4.0-branch/", 1058});
		urls.add(new Object[]{"https://landfill.bugzilla.org/bugzilla-4.2-branch/", 1058});
		urls.add(new Object[]{"https://landfill.bugzilla.org/bugzilla-4.4-branch/", 1058});
		
		return urls;
	}
	
	private String url;
	
	private int id;
	
	public GetBugHistoryIT(String url, int id) {
		this.url = url;
		this.id = id;
	}
	
	@Test
	public void testGetBugHistory() throws ConnectionException, BugzillaException, ParseException {
		BugzillaConnector conn = new BugzillaConnector();
		conn.connectTo(url);
		
		GetBugHistory get = new GetBugHistory(id);
		conn.executeMethod(get);
		
		List<HistoryElement> history = get.getBugHistory();
		
		System.out.println();
		assertThat("Should return history of a bug.", history, notNullValue());
		
		for (HistoryElement hElem : history)
		{
			System.out.println(hElem.getUser());
			System.out.println(hElem.getChangeTime().toString());
			System.out.println();
			for (HistoryElement.Change change : hElem.getChanges())
			{
				System.out.println("\t" + change.fieldName());
				System.out.println("\t" + change.added());
				System.out.println("\t" + change.removed());
				
			}
		}
	}
}
